﻿using System;

namespace YoutubeDownloader
{
    public class DownloadItem
    {
        public Video Video;
        public VideoLink VideoLink;
        public long StartTime;
        public int Progress;
        public long DownloadedByte;
        public long TotalByte;
        public DownloadEvent Event;

        private int _lastRemainingTime;

        public enum DownloadEvent
        {
            DownloadQueued,
            DownloadStarted,
            Downloading,
            DownloadEnded,
            DownloadFailed
        }

        static string BytesToString(long bytes)
        {
            string bytesToString = "";

            if (bytes < 1024)
            {
                bytesToString = bytes + "b";
            }
            else if (bytes < 1024 * 1024)
            {
                bytesToString = (bytes / 1024) + "Kb";
            }
            else if (bytes < 1024 * 1024 * 1024)
            {
                bytesToString = string.Format("{0},{1:D3}Mb", bytes / (1024 * 1024), (bytes / 1024) % 1024);
            }

            return bytesToString;
        }

        override public string ToString()
        {
            string message;
            switch (Event)
            {
                case DownloadEvent.DownloadEnded:
                    message = "Download Ended, " + VideoLink.FormatDescription;
                    break;
                case DownloadEvent.DownloadStarted:
                    message = "Download Started, " + VideoLink.FormatDescription;
                    break;
                case DownloadEvent.DownloadFailed:
                    message = "Download Failed";
                    break;
                case DownloadEvent.Downloading:
                    {
                        double timeElapsed = (System.Environment.TickCount - StartTime)/1000.0;
                        double remainingTime = double.MaxValue;
                        double progress = ((double)DownloadedByte/TotalByte)*100;
                             
                        if (Progress != 0)
                        {
                            remainingTime = (timeElapsed/progress)*(100 - progress);
                        }

                        // Do not notify too much
                        if (_lastRemainingTime != (int) remainingTime && ((int) (remainingTime/60) >= 0))
                        {
                            _lastRemainingTime = (int) remainingTime;
                        }

                        string etc = string.Format("{0:0.00}% : ETC {1}:{2:00}s", Math.Round(progress, 2),
                                                           (int)(_lastRemainingTime / 60), (int)(_lastRemainingTime % 60));

                        message = string.Format("Format: {0} - ETC: {1} ({2}/{3})", VideoLink.FormatDescription, etc, BytesToString(DownloadedByte), BytesToString(TotalByte));
                    }
                    break;
                default:
                    message = "Download State Unknown";
                    break;
            }
            return message;
        }
    }
}
